package com.ftl.autopunch.service.impl;

import com.ftl.autopunch.service.PunchService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Objects;

@Service
public class PunchServiceImpl implements PunchService {

    @Override
    public boolean punchAction(String serverSessionId, String userSessionId) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url("http://xxtj.shufe-zj.edu.cn/Punch/Punch.aspx?Action=NoChange")
                .method("GET", null)
                .addHeader("Cookie", "SessionId=" + userSessionId + "; ASP.NET_SessionId=" + serverSessionId)
                .build();
        Response response = client.newCall(request).execute();
        try {
            String res = Objects.requireNonNull(response.body()).string();
            System.out.println(res);
            return res.contains("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
